package com.skar.childtacker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.skar.childtacker.db.DbOpenHelper;
import com.skar.childtacker.db.FeedTable;
import com.skar.childtacker.entity.Feed;

import java.util.Date;
import java.util.List;

public class FeedActivity extends Activity {

    private DbOpenHelper dbOpenHelper;
    private FeedTable feedTable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed_layout);

        dbOpenHelper = new DbOpenHelper(this);
        feedTable = new FeedTable(dbOpenHelper);

        findViewById(R.id.feed_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Feed feed = new Feed();
                feed.setStartDate(new Date());
                feed.setFinishDate(feed.getStartDate());
                feedTable.save(feed);
                updateFeedList();
            }
        });
    }

    protected void updateFeedList() {
        ListView listView = (ListView) findViewById(R.id.feed_list);
        listView.removeAllViews();
        List<Feed> feeds = feedTable.getAll();
        listView.setAdapter(new ArrayAdapter<Feed>(this, R.layout.feed_item, feeds));
    }

}
